﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VSA.Applications.VSA.WebService;

namespace VSA.Applications
{
    public partial class ProjectInfoForm : Form
    {
        private InfoEditGuideForm guide2AddForm = new InfoEditGuideForm();
        MainService _ProjectService = WebServiceFactory.GetService();
        DataTable _ProjectDataSource = new DataTable();
        List<int> _DeletedIdList = new List<int>();
        public PersonInfoForm ContactPersonForm { get; set; }

        public ProjectInfoForm()
        {
            InitializeComponent();
            gv_Projects.AutoGenerateColumns = false;
            _ProjectService.GetProjectListAsync("");
            _ProjectService.GetProjectListCompleted += new GetProjectListCompletedEventHandler(GetProjectListCompleted);
        }

        void GetProjectListCompleted(object sender, GetProjectListCompletedEventArgs e)
        {
            gv_Projects.DataSource = e.Result.Tables[0];
            _ProjectDataSource = gv_Projects.DataSource as DataTable;
        }

        void SubmitUpdate()
        {
            foreach (DataRow dr in _ProjectDataSource.Rows)
            {
                if (dr.RowState == DataRowState.Added)
                {
                    Project project = new Project();
                    project.ProjectName = dr["ProjectName"].ToString();
                    project.CreateDate = DateTime.Now;
                    project.Note = dr["Note"].ToString();
                    _ProjectService.AddProject(project);
                }
                else if (dr.RowState == DataRowState.Modified)
                {
                    Project project = new Project();
                    project.ProjectId = int.Parse(dr["ProjectId"].ToString());
                    project.Note = dr["Note"].ToString();
                    project.ProjectName = dr["ProjectName"].ToString();
                    _ProjectService.UpdateProject(project);
                }
               
                foreach (int did in _DeletedIdList)
                {
                    _ProjectService.DeleteProject(did);
                }
                _DeletedIdList.Clear();
            }
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            if (this.ContactPersonForm == null)
            {
                guide2AddForm.Show();
            }
            this.Dispose();
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                SubmitUpdate();

                if (this.ContactPersonForm != null)
                {
                    DataTable dt = _ProjectService.GetProjectList("").Tables[0];
                    this.ContactPersonForm.BuildProjectCheckBox(dt);
                    this.Dispose();
                }

                MessageBox.Show(
                    "Update successfully",
                    "System Information",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information
                );
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    ex.Message,
                    "System Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }

        private void txt_ProjectName_Validated(object sender, EventArgs e)
        {
            TextBox txtBox = (TextBox)sender;
            this.errorProvider1.SetError(txtBox, "");
        }

        private void txt_ProjectName_Validating(object sender, CancelEventArgs e)
        {
            string errorMsg = string.Empty;
            TextBox txtBox = (TextBox)sender;
            if (string.IsNullOrEmpty(txtBox.Text))
            {
                e.Cancel = true;
                txtBox.Select(0, txtBox.Text.Length);
                this.errorProvider1.SetError(txtBox, "Please input project name.");
            }
        }

        private void gv_Projects_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            _DeletedIdList.Add(int.Parse(e.Row.Cells[0].Value.ToString()));
        }

        private void ProjectInfoForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.ContactPersonForm == null)
            {
                guide2AddForm.Show();
            }
            this.Dispose();
        }
    }
}
